import chalk from "chalk";
import { readFile } from "fs/promises";
import cosmiconfig from "cosmiconfig";
const { cosmiconfigSync } = cosmiconfig;
const pkg = JSON.parse(
  await readFile(new URL("../../package.json", import.meta.url))
);
const configLoader = cosmiconfigSync(pkg.name);
const defaultConfig = {
  types: [
    {
      name: "前端项目",
      value: "frontend",
      gitTemplateUrl:
        "http://172.16.240.4/common-code/template/frontend-main.git",
      description: "选择前端模板项目",
    },
    {
      name: "后端项目",
      value: "backend",
      gitTemplateUrl:
        "http://172.16.240.4/common-code/template/backend/multi-module-main.git",
      multiModule: true,
      mainModule: "main-entrance",
      description: "选择后端模板项目",
    },
  ],
  modules: [
    {
      name: "登录鉴权模块",
      value: "sys-auth",
      type: "backend",
      gitTemplateUrl:
        "http://172.16.240.4/common-code/template/backend/sys-auth.git",
      description:
        "登录鉴权模块后端部分，登录鉴权模块包含需要登录鉴权的各项配置，可以配合用户管理模块使用，也可接入外部权限配置使用。",
    },
    {
      name: "登录鉴权模块",
      value: "sys-auth",
      type: "frontend",
      gitTemplateUrl:
        "http://172.16.240.4/common-code/template/frontend/sys-auth.git",
      description: "登录鉴权模块前端部分。",
    },
  ],
  mappings: {
    java: {
      INT: "Integer",
      BIGINT: "Long",
      VARCHAR: "String",
      TEXT: "String",
      DATETIME: "LocalDateTime",
      DATE: "LocalDate",
      TIME: "LocalTime",
      DECIMAL: "BigDecimal",
      FLOAT: "Float",
      DOUBLE: "Double",
      BOOLEAN: "Boolean",
      BLOB: "byte[]",
      ENUM: "String",
    },
    typescript: {
      INT: "number",
      BIGINT: "string",
      VARCHAR: "string",
      TEXT: "string",
      DATETIME: "date",
      DATE: "date",
      TIME: "date",
      DECIMAL: "number",
      FLOAT: "number",
      DOUBLE: "number",
      BOOLEAN: "boolean",
      BLOB: "byte[]",
      ENUM: "string",
    },
  },
  templates: [
    {
      name: "Jimmer Model文件",
      value: "jimmer-model",
      type: "backend",
      description: "用于生成Jimmer框架的实体类",
      path: "src/main/java/com/jezetek/model",
      package: "com.jezetek.model"
    },
    {
      name: "Jimmer Repository文件",
      value: "jimmer-repository",
      type: "backend",
      description: "用于生成Jimmer框架的数据接口类",
      path: "src/main/java/com/jezetek/repository",
      package: "com.jezetek.repository"
    },
    {
      name: "Jimmer DTO文件",
      value: "jimmer-dto",
      type: "backend",
      description: "用于生成Jimmer框架的DTO文件",
      path: "src/main/dto",
      package: "com.jezetek.model.dto"
    },
    {
      name: "Jimmer Controller类",
      value: "jimmer-controller",
      type: "backend",
      description: "用于生成Jimmer框架的Controller类",
      path: "src/main/java/com/jezetek/controller",
      package: "com.jezetek.controller"
    },
    {
      name: "Jimmer 接口测试类",
      value: "jimmer-test-controller",
      type: "backend",
      description: "用于生成Jimmer框架的接口测试类",
      path: "src/test/java/com/jezetek/controller",
      package: "com.jezetek.controller"
    },
    {
      name: "TDesign Vue组件的增删改查界面",
      value: "ant-design-vue-curd",
      type: "frontend",
      description: "用于生成TDesign UI组件的增删改查界面",
      path: "src/views"
    }
  ],
};

export default {
  getConfig: () => {
    const result = configLoader.search(process.cwd());
    if (!result) {
      console.log(chalk.yellow("Could not find configuration, using default"));
      return defaultConfig;
    } else {
      return {
        ...defaultConfig,
        ...result.config,
      };
    }
  },
};
